package com.example.animdemo.rigidanim;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.example.animdemo.base.DebugLog;

public class RigidFactory {
	private static final String WRONG_VESION = "Wrong version!";
    private static final String FILE_DAMAGED = "File Damaged!";
    private static final String DESCRIPTION = "Animation Builder v1.0";
	
	public static RigidBody rigidBodyFrom(InputStream stream) {
    	RigidBody body = null;
    	try {
    		DataInputStream sr = new DataInputStream(stream);
    		String buffer = null;
    		
    		// Checking description.
    		buffer = sr.readLine();
    		if(buffer == null || !buffer.equals(DESCRIPTION))
    			throw new Exception(WRONG_VESION);
    		
    		buffer = sr.readLine();
    		
    		// Checking content.
            buffer = sr.readLine();
            if (buffer == null || !buffer.contains("NUMJOINT"))
            	throw new Exception(FILE_DAMAGED);
            
            buffer = buffer.substring(8).trim();
            
            int numJoint = Integer.parseInt(buffer);
            
            sr.readLine(); // ""
            sr.readLine(); // "Hierachy"
            sr.readLine(); // "{"
            
            RigidJoint[] skeleton = new RigidJoint[numJoint];
            
            for(int jointId = 0; jointId < numJoint; jointId++) {
            	buffer = sr.readLine().trim();
                String[] tokens = buffer.split(" ");
                RigidJoint joint = new RigidJoint();
                
                joint.name = tokens[0].trim();
                joint.jointId = Integer.parseInt(tokens[1]);
                joint.parentId = Integer.parseInt(tokens[2]);
                joint.objectID = Integer.parseInt(tokens[3]);

                joint.jointInfo.posX = Float.parseFloat(tokens[4]);
                joint.jointInfo.posY = Float.parseFloat(tokens[5]);
                joint.jointInfo.selfRot = Float.parseFloat(tokens[6]);
                joint.jointInfo.rot = Float.parseFloat(tokens[7]);
                joint.jointInfo.scaleX = Float.parseFloat(tokens[8]);
                joint.jointInfo.scaleY = Float.parseFloat(tokens[9]);

                skeleton[jointId] = joint;
            }
            
            body = new RigidBody(skeleton);
    	} catch (IOException e) {
    		body = null;
    		DebugLog.e("AnimDemo", "ReadModel:" + e.getMessage());
    	} catch (Exception e) {
    		body = null;
    		DebugLog.e("AnimDemo", "ReadModel:" + e.getMessage());
		} 
    	
		return body;
    }
	
	public static final RigidSoul rigidSoulFrom(InputStream stream) {
		RigidSoul soul = null;
		try {
    		DataInputStream sr = new DataInputStream(stream);
    		String buffer = null;
    		
    		sr.readLine();
    		sr.readLine();
    		
    		// Checking content.
            buffer = sr.readLine();
            if (buffer == null || !buffer.contains("NUMJOINT"))
                throw new Exception(FILE_DAMAGED);

            buffer = buffer.substring(8).trim();
            int numJoint = Integer.parseInt(buffer);

            buffer = sr.readLine();
            if (buffer == null || !buffer.contains("TIMELIFE"))
            	throw new Exception(FILE_DAMAGED);
            
            buffer = buffer.substring(8).trim();
            final float timeLife = Float.parseFloat(buffer);
            sr.readLine();
            
            final RigidJointInfo[][] frames = new RigidJointInfo[numJoint][];
            
            for(int jointIdx = 0; jointIdx < numJoint; jointIdx++) {
            	sr.readLine();
                buffer = sr.readLine().trim();
                String[] tokens = buffer.split(" ");
                int numFrames = Integer.parseInt(tokens[1]);
                final RigidJointInfo[] infos = new RigidJointInfo[numFrames];
                sr.readLine();
                for (int frameIdx = 0; frameIdx < numFrames; frameIdx++)
                {
                	final RigidJointInfo frame = new RigidJointInfo();
                    
                    buffer = sr.readLine().trim();
                    tokens = buffer.split(" ");

                    frame.time = Float.parseFloat(tokens[0]);
                    frame.t = Float.parseFloat(tokens[1]);
                    frame.frame = Integer.parseInt(tokens[2]);
                    frame.posX = Float.parseFloat(tokens[3]);
                    frame.posY = Float.parseFloat(tokens[4]);
                    frame.selfRot = Float.parseFloat(tokens[5]);
                    frame.rot = Float.parseFloat(tokens[6]);
                    frame.scaleX = Float.parseFloat(tokens[7]);
                    frame.scaleY = Float.parseFloat(tokens[8]);
                    
                    infos[frameIdx] = frame;
                }
                frames[jointIdx] = infos;
                sr.readLine();
            }
            
            soul = new RigidSoul(frames, timeLife);            
    	} catch (IOException e) {
    		soul = null;
    		DebugLog.e("AnimDemo", "ReadSoul:" + e.getMessage());
    	} catch (Exception e) {
    		soul = null;
    		DebugLog.e("AnimDemo", "ReadSoul:" + e.getMessage());
		} 
		
		return soul;
	}
}

